var port = require('../../modules/port.js');
var pageNum = 1;
var requestStatus = true;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchText: '',
        searchValue: '',
        emptySwitch: false,
        searchContent: false,
        scrollHeight: 0,
        scrollTop: '',
        tagList: [],
        searchData: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        // 获取设备高度
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    scrollHeight: res.windowHeight - 44,
                });
            }
        });
        wx.request({
            url: port.dataPort.tagList,
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
            // header: {}, // 设置请求的 header    
            success: function (data) {
                that.setData({
                    tagList: data.data.data
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindLoadMore: function () {
        var that = this;
        if (requestStatus) {
            requestStatus = false;
            pageNum++;
            wx.showNavigationBarLoading();
            wx.request({
                url: port.dataPort.searchData,
                data: { content: this.data.searchValue, pageNum: pageNum },
                method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
                // header: {}, // 设置请求的 header    
                success: function (data) {
                    var idx = '';
                    var listData = data.data.data;
                    var vi;
                    for (vi in listData) {
                        listData[vi].like = that.numberSwitch(listData[vi].like);
                    }
                    for (idx in data.data.data) {
                        that.data.searchData.push(data.data.data[idx]);
                    }
                    that.setData({
                        searchData: that.data.searchData,
                        searchContent: true
                    })
                    requestStatus = true;
                    wx.hideNavigationBarLoading();
                }
            })
        }
    },
    articleTo: function (e) {
        wx.navigateTo({
            url: '../article/article?id=' + e.currentTarget.dataset.id
        })
    },
    searchRequest: function (content) {
        var that = this;
        pageNum = 1;
        wx.showNavigationBarLoading();
        wx.request({
            url: port.dataPort.searchData,
            data: { content: content, pageNum: 1 },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
            // header: {}, // 设置请求的 header    
            success: function (data) {
                var listData = data.data.data;
                var vi;
                for (vi in listData) {
                    listData[vi].like = that.numberSwitch(listData[vi].like);
                }
                that.setData({
                    searchData: data.data.data,
                    searchContent: true,
                    searchValue: content
                })
                wx.hideNavigationBarLoading();
            }
        })
    },
    searchText: function (e) {
        if (e.detail.value.length > 0) {
            this.setData({
                searchText: e.detail.value,
                emptySwitch: true
            })
        } else {
            this.setData({
                searchText: e.detail.value,
                emptySwitch: false
            })
        }
    },
    tagBtn: function (e) {
        this.searchRequest(e.currentTarget.dataset.text)
    },
    searchBtn: function () {
        this.searchRequest(this.data.searchText)
    },
    searchEmpty: function () {
        this.setData({
            searchText: '',
            emptySwitch: false
        })
    },
    numberSwitch: function (str) {
        if (str >= 10000) {
            str = str / 10000;
            return str.toFixed(1) + '万';
        } else {
            return str;
        }
    }
})